<?php

namespace app\admin\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use think\response\Json;

class SystemConfig extends AdminBase
{
    public function getData(): Json
    {
        try {
            $systemConfigBis = new \app\common\business\SystemConfig();
            $data=$systemConfigBis->getListByKeys(['commission_rate','earnest_rate']);
            $data =$systemConfigBis->getResetInfo($data);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($data);
    }

    public function setData(): Json
    {
        list($commission_rate, $earnest_rate) = Param::postMore([
            ['commission_rate',0],
            ['earnest_rate', 0],
        ], $this->request, true);

        $data=[
            'commission_rate'=>$commission_rate,
            'earnest_rate'=>$earnest_rate,
        ];
        $validate = new \app\admin\validate\SystemConfig();
        if (!$validate->check($data)) {
            return Show::error([], $validate->getError());
        }
        try {
            $result = (new \app\common\business\SystemConfig())->save($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }
}
